<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="公告标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入公告标题" />
      </el-form-item>
      <el-form-item label="公告内容" prop="content">
        <Editor v-model="formData.content" height="150px" />
      </el-form-item>
      <el-form-item label="发布人ID" prop="publisherId">
        <el-input v-model="formData.publisherId" placeholder="请输入发布人ID" />
      </el-form-item>
      <el-form-item label="发布人姓名" prop="publisherName">
        <el-input v-model="formData.publisherName" placeholder="请输入发布人姓名" />
      </el-form-item>
      <el-form-item label="发布部门ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入发布部门ID" />
      </el-form-item>
      <el-form-item label="发布部门名称" prop="deptName">
        <el-input v-model="formData.deptName" placeholder="请输入发布部门名称" />
      </el-form-item>
      <el-form-item label="公告类型：1普通公告 2重要公告 3紧急公告" prop="announcementType">
        <el-select v-model="formData.announcementType" placeholder="请选择公告类型：1普通公告 2重要公告 3紧急公告">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否置顶(1是 0否)" prop="isTop">
        <el-radio-group v-model="formData.isTop">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="置顶到期时间" prop="topExpiry">
        <el-date-picker
          v-model="formData.topExpiry"
          type="date"
          value-format="x"
          placeholder="选择置顶到期时间"
        />
      </el-form-item>
      <el-form-item label="生效时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择生效时间"
        />
      </el-form-item>
      <el-form-item label="失效时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          value-format="x"
          placeholder="选择失效时间"
        />
      </el-form-item>
      <el-form-item label="查看次数" prop="viewCount">
        <el-input v-model="formData.viewCount" placeholder="请输入查看次数" />
      </el-form-item>
      <el-form-item label="是否已通知(1是 0否)" prop="isNotified">
        <el-radio-group v-model="formData.isNotified">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="附件URL(多个用逗号分隔)" prop="attachments">
        <UploadFile v-model="formData.attachments" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="formData.createTime"
          type="date"
          value-format="x"
          placeholder="选择创建时间"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker
          v-model="formData.updateTime"
          type="date"
          value-format="x"
          placeholder="选择更新时间"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          v-model="formData.publishTime"
          type="date"
          value-format="x"
          placeholder="选择发布时间"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-input v-model="formData.creator" placeholder="请输入创建者" />
      </el-form-item>
      <el-form-item label="更新者" prop="updater">
        <el-input v-model="formData.updater" placeholder="请输入更新者" />
      </el-form-item>
      <el-form-item label="是否删除" prop="deleted">
        <el-radio-group v-model="formData.deleted">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="租户编号" prop="tenantId">
        <el-input v-model="formData.tenantId" placeholder="请输入租户编号" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OaAnnouncementApi, OaAnnouncementVO } from '@/api/bpm/oaannouncement'

/** 企业公告 表单 */
defineOptions({ name: 'OaAnnouncementForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  title: undefined,
  content: undefined,
  publisherId: undefined,
  publisherName: undefined,
  deptId: undefined,
  deptName: undefined,
  announcementType: undefined,
  status: undefined,
  isTop: undefined,
  topExpiry: undefined,
  startTime: undefined,
  endTime: undefined,
  viewCount: undefined,
  isNotified: undefined,
  attachments: undefined,
  createTime: undefined,
  updateTime: undefined,
  publishTime: undefined,
  creator: undefined,
  updater: undefined,
  deleted: undefined,
  tenantId: undefined,
})
const formRules = reactive({
  title: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }],
  content: [{ required: true, message: '公告内容不能为空', trigger: 'blur' }],
  publisherId: [{ required: true, message: '发布人ID不能为空', trigger: 'blur' }],
  publisherName: [{ required: true, message: '发布人姓名不能为空', trigger: 'blur' }],
  announcementType: [{ required: true, message: '公告类型：1普通公告 2重要公告 3紧急公告不能为空', trigger: 'change' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
  isTop: [{ required: true, message: '是否置顶(1是 0否)不能为空', trigger: 'blur' }],
  viewCount: [{ required: true, message: '查看次数不能为空', trigger: 'blur' }],
  isNotified: [{ required: true, message: '是否已通知(1是 0否)不能为空', trigger: 'blur' }],
  createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
  updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
  deleted: [{ required: true, message: '是否删除不能为空', trigger: 'blur' }],
  tenantId: [{ required: true, message: '租户编号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OaAnnouncementApi.getOaAnnouncement(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OaAnnouncementVO
    if (formType.value === 'create') {
      await OaAnnouncementApi.createOaAnnouncement(data)
      message.success(t('common.createSuccess'))
    } else {
      await OaAnnouncementApi.updateOaAnnouncement(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    title: undefined,
    content: undefined,
    publisherId: undefined,
    publisherName: undefined,
    deptId: undefined,
    deptName: undefined,
    announcementType: undefined,
    status: undefined,
    isTop: undefined,
    topExpiry: undefined,
    startTime: undefined,
    endTime: undefined,
    viewCount: undefined,
    isNotified: undefined,
    attachments: undefined,
    createTime: undefined,
    updateTime: undefined,
    publishTime: undefined,
    creator: undefined,
    updater: undefined,
    deleted: undefined,
    tenantId: undefined,
  }
  formRef.value?.resetFields()
}
</script>
